package org.soarsky.wealthchengdu.util;

import java.io.ByteArrayOutputStream;
import java.io.File;

import android.graphics.Bitmap;
import android.os.Environment;
import android.util.Log;

/**
 * 全局公共类
 * @author yue
 *
 */
public class Utils {	

	/**
	 * 屏幕宽
	 */
	public static int			mScreenWidth	= 0;
	/**
	 * 屏幕高
	 */
	public static int			mScreenHeight	= 0;

	/**
	 * 
	 * 作用：
	 * <p>
	 * 设置屏幕宽高
	 * </p>
	 * 
	 * @param width
	 * @param height
	 */
	public static void setScreenSize(int width, int height)
	{
		mScreenWidth = width;
		mScreenHeight = height;
	}

	/**
	 * 
	 * @param context
	 *            上下文
	 * @param subFolder
	 *            cache的文件夹
	 * @return
	 */
	public static String getCacheDir(String subFolder) {
		String rootDir = null;
		if (isSDCardEnable()) {
			// SD-card available
			rootDir = Environment.getExternalStorageDirectory().getAbsolutePath() + Config.sdcardtemppic;
//			rootDir = Config.sdcardtemppic;
			Log.e("ggggg", "the path: "+rootDir);

			// 创建文件目录
			File file = new File(rootDir);
			if (!file.exists()) // 创建目录
			{
				file.mkdirs();
			}
		}

		return rootDir;
	}	

	/**
	 * SD是否可用
	 * 
	 * @return
	 */
	public static boolean isSDCardEnable() {		
		return android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);
	}

	/**
	 * 验证url的合法性
	 * @param url
	 * @return
	 */
	public static boolean checkUrl(String url) {
		return url.matches("^[a-zA-z]+://[^\\s]*$");
	}

	/**
	 * 对数据进行base64编码
	 * @param name
	 * @return
	 */
	public static final String encodeBase64(String name) {
		try {
			return Base64.encode(name);
		} catch (Exception e) {			
		}
		return name != null ? name : "";
	}

	/*
	 * 提取url中的参数
	 */
	public static String getParameter(String url, String name) {
		if(url ==null || name == null) {
			return null;
		}

		int start = url.indexOf(name + "=");
		if(start == -1)
			return null;
		int len = start + name.length() + 1;
		int end = url.indexOf("&", len);
		if(end == -1) {
			end = url.length();
		}

		return url.substring(len, end);
	}

	/**
	 * 转换积分格式
	 * @param point
	 * @return newpoint
	 */
	public static String formatPoint( String point ){
		try
		{
			String newpoint = point.replaceAll(",", ""); // 首先去掉所有逗号
			java.text.DecimalFormat df = new java.text.DecimalFormat("##,###,###");
			return df.format( Double.parseDouble(newpoint) );
		}
		catch(Exception e)
		{
			return point;
		}
	}
	
	/**
	 * bitmap转换
	 * @param bm
	 * @return
	 */
	public static byte[] bitmap2Bytes(Bitmap bm) {
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		bm.compress(Bitmap.CompressFormat.PNG, 100, baos);
		return baos.toByteArray();
	}
	
}